package com.example.demo.dao;


import com.example.demo.domain.Department;
import com.example.demo.domain.Student;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("studentDao")
public class StudentDao {
    private List<Student> studentList = new ArrayList<Student>();
    //增加
    public List<Student> addStudent(Student student){
        studentList.add(student);
        return studentList;
    }
    //批量增加
    public List<Student> addStudents(List<Student> students){
        studentList.addAll(students);
        return studentList;
    }
    //查询
    public List<Student> getAll(){
        return studentList;
    }
    public Student getOneByNo(String no){
        Student student = null;
        if(studentList.size()==0){
            return null;
        }else {
            for(int i = 0;i<studentList.size();i++){
                if(studentList.get(i).getNo().equals(no)){
                    student=studentList.get(i);
                    break;
                }
            }
        }
        return student;
    }
    //更新
    public boolean updateStudent(Student student){
        boolean flag = false;
        if(studentList.size()!=0){
           for (int i = 1;i<studentList.size();i++){
               if(studentList.get(i).getNo().equals(student.getNo())){
                   studentList.get(i).setName(student.getName());
                   studentList.get(i).setRemarks(student.getRemarks());
                   flag = true;
                   break;
               }
           }
        }
        return flag;
    }
    //删除
    public boolean deleteStudent(String no){
        boolean flag = false;
        if(studentList.size()==0){
            return flag;
        }else {
            for(int i = 1;i<studentList.size();i++){
                if(studentList.get(i).getNo().equals(no)){
                 studentList.remove(i);
                 flag = true;
                    break;
                }
            }
        }
        return flag;
    }
}
